package com.yanxu.threadandobjectmethods;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/3/20-2025
 * @Description: wait 只能释放当前的锁
 */
public class WaitOnlyReleaseCurrentLock {
    //第一两把锁
    static Object lock1=new Object();
    static Object lock2=new Object();

    public static void main(String[] args) throws InterruptedException {

        //第一个线程
        new Thread(new Runnable() {
            public void run() {
                synchronized (lock1) {
                    System.out.println(Thread.currentThread().getName()+" 获取到了第一把锁!!!");
                    synchronized (lock2){
                        System.out.println(Thread.currentThread().getName()+" 获取到了第二把锁!!!");
                        try {
                            lock1.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        },"线程1").start();

        Thread.sleep(200);

        //第二个线程
        new Thread(new Runnable() {
            public void run() {
                synchronized (lock1) {
                    System.out.println(Thread.currentThread().getName()+" 获取到了第一把锁!!!");
                    synchronized (lock2){
                        System.out.println(Thread.currentThread().getName()+" 获取到了第二把锁!!!");
                    }
                }
            }
        },"线程2").start();
    }
}